<?php
class Tests_Model extends MY_Model
{
    protected $_table_name = 'tests';

	public function __construct() {
		parent::__construct();
	}	
	
	public function clean_duration($id){
		$this->db->delete('test_duration', array('id'=>$id));
		//echo $this->db->last_query();
	}
	
	public function stop_test($test_id){
		$data = array( 'start' => 0 );
		$this->db->where('id', $test_id);
		$this->db->update($this->_table_name, $data); 
	}
	
	public function start_duration($test_id, $duration){
		$this->db->delete('test_duration',array('test_id' => $test_id));
		$sql = "INSERT INTO test_duration(test_id, duration, start_time, ip) VALUES(" . $test_id . "," . $duration .",'" . date('H:i:s') ."','" .  $_SERVER['REMOTE_ADDR']. "')";
		$this->db->query($sql);
		return $this->db->insert_id();
	}
	
	public function stop_duration($test_id){
		$this->db->delete('test_duration',array('test_id' => $test_id));
	}
	
	public function get_start_time($test_id){
		$this->db->where('test_id',$test_id);
		$rs = $this->db->get('test_duration');
		$rs = $rs->result_array();
		if(count($rs) > 0) {
			return $rs[0]['start_time'];
		}
		else
			return 0;
		
	}
	
	public function get_chapter_name($question_id){
		$this->db->where('id',$question_id);
		$rs = $this->db->get('questions');
		$rs = $rs->result_array();
		//echo $this->db->last_query();
		return $rs[0]['chapter_name'];
	}
	
	public function get_qty($question_id){
		$this->db->where('id',$question_id);
		$rs = $this->db->get('questions');
		$rs = $rs->result_array();
		return $rs[0]['qty'];
	}
	
	public function get_questions($question_id=0, $percentage=100){
		if(isset($_SESSION['question_id']) && isset($_SESSION['percentage'])) {
			//$question_id = explode($_SESSION['question_id'],",");
			//$percentage = explode($_SESSION['percentage'],",");
		}
		
		$this->db->where('question_id',$question_id);
		$total_question = 3; //$this->db->count_all_results();
		if($percentage<100)
			$rs = $this->db->get('questions_detail',round(($percentage/100) * $total_question), 0);
		else
			$rs = $this->db->get('questions_detail');
		$rs = $rs->result_array();
		return $rs;
		
	}
	
	public function get_tests($school_year_id, $class_id, $test_id=0){
		$this->db->where('school_year_id',$school_year_id);
		$this->db->where('start',1);
		if($test_id>0)
			$this->db->where('id',$test_id);
		$rs = $this->db->get($this->_table_name)->result_array();
		
		$temp = "";
		$data = array();
		foreach($rs as $row){
			$classes = explode(",",$row['class_id']);
			//echo count($classes) . " jumlah kelas";
			for($i=0; $i< count($classes); $i++){
				//echo var_dump($classes) . " " . $class_id . "<BR>";
				if($classes[$i] == $class_id) {
					$temp = $classes[$i];
					break;
				}
			}
			if($temp!="") {
				$row['class_id'] = $temp;
				array_push($data,$row);
			}
			$temp = "";
		}
		//echo var_dump($data);
		return $data;
	}
	
	public function get_tests_info($school_year_id, $class_id, $test_id=0){
		$this->db->where('school_year_id', $school_year_id);
		$this->db->where('class_id', $class_id);
		if($test_id>0)
			$this->db->where('id',$test_id);
			
		$rs = $this->db->get($this->_table_name)->result_array();
		return $rs;
	}
	
	public function get_marks($test_id, $nis){
		$this->db->where('id', $test_id);
		$rs = $this->db->get('tests')->result_array();
		$total_qty = $rs[0]['total_qty'];
		$max_value = $rs[0]['maximum_value'];
		
		$this->db->where('nis',$nis);
		$this->db->where('test_id',$test_id);
		$this->db->where('judge',1);
		$total_right_answer = $this->db->count_all_results('test_question_student');
		
		$point_per_question = $max_value / $total_qty;
		return round($point_per_question * $total_right_answer);
	}
}